package com.project.quiktask.controller;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.sql.Date;
import java.sql.Time;
import java.text.ParseException;
import java.util.Collections;
import java.util.Vector;

import com.project.quiktask.datastore.FileLoader;
import com.project.quiktask.util.Result;
import com.project.quiktask.util.Variable;
import com.project.quiktask.Task;
/**
 * 
 * @author PHAM MINH VET
 *
 */
public class SearchHandler implements CommandHandler{

	public SearchHandler() {


	}

	@Override
	public Result execute(String parameter) {
		Vector<Task> result=new Vector<Task>();
		Result answer = null;
		FileLoader loader;
		Vector<Task> taskContainer=new Vector<Task>();
		try {
			loader = new FileLoader(Variable.STORAGE_FILE_NAME);
			taskContainer = loader.load();
		} catch (FileNotFoundException e) {
			return new Result("File not found");
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		if(parameter.startsWith("by")){
			String date=parameter.replace("by", "").trim();
			String[] fields=date.split(" ");
			if(fields.length==1){
				Date deadline=DateTimeUtil.tryParseDate(date);
				result=searchByDeadlineDate(deadline, taskContainer);	
			}else if(fields.length==2){
				Time deadlineTime=DateTimeUtil.tryParseTime(fields[0]);
				Date deadlineDate=DateTimeUtil.tryParseDate(fields[1]);
				//still have to figure out how to compare time in java 
			}
			
		} else {
			result = searchByKeyword(parameter, taskContainer);
		}	
		int size = result.size();
		answer = new Result(size+" tasks found", result);
		return answer;
	}
	public Vector<Task> searchByKeyword(String keyword,Vector<Task> tasks) {//throws SearchNotFoundException 
		Vector<Task> answer;
		answer = new Vector<Task>();
		for(int i = 0; i < tasks.size(); i++){
			Task task = tasks.get(i);
			if(task.getTaskDescription()!=null){
				String taskDescriptionLowercase=task.getTaskDescription().toLowerCase();
				if(taskDescriptionLowercase.contains(keyword.toLowerCase())){
					answer.add(task);
				}
			}
		}
			return answer;
		}

		public Vector<Task> searchByDeadlineDate(Date deadline,Vector<Task> tasks){
			Vector<Task> answer=new Vector<Task>();
			for(int i=0;i<tasks.size();i++){
				Task task=tasks.get(i);
				if(task.getDeadlineDate()!=null){
					if(task.getDeadlineDate().before(deadline)){
						answer.add(task);
					}
			}
			}
			Collections.sort(answer, new DeadlineComparator());
			return answer;
		}
		
		public Vector<Task> searchByDeadline(Date deadlineDate,Time deadlineTime,Vector<Task> tasks){
			
			Vector<Task> answer=new Vector<Task>();
			for(int i=0;i<tasks.size();i++){
				Task task=tasks.get(i);
				if(task.getDeadlineDate()!=null){
					if(task.getDeadlineDate().before(deadlineDate)){
						answer.add(task);
					}else if(task.getDeadlineDate().equals(deadlineDate)){
						if(task.getDeadlineTime()!=null){
						//	if(task.getDeadlineTime().)
						}
					}
			}
			}
			Collections.sort(answer, new DeadlineComparator());
			return answer;
		}

		@Override
	public Result undo() {
		return new Result(Variable.CANNOT_UNDO_SEARCH_COMMAND);
		}

	}
